﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="School.aspx.cs" Inherits="Education_School" %>

<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>学校管理</title>
    
    <link href="../Common/css/main.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        #load{width:500px;height:25px;border:1px #000 solid;}
        #loading{position:absolute;z-index:2;height:23;filter:progid:DXImageTransform.microsoft.gradient(gradienttype=1,startColorStr=white,endColorStr=#39867b);}
        #loadtext{position:absolute;z-index:3;width:100%;height:100%;line-height:23px;text-align:center;}
    </style>
    
    <script type="text/javascript" src="../Common/Js/jquery.js"></script>

    <script type="text/javascript" src="../Common/Js/common.js"></script>

    <script type="text/javascript" src="../Common/Js/msg.js"></script>

    <script type="text/javascript" src="../Common/Js/jsUtil.js"></script>
    
    <script type="text/javascript">
    
var optionType = '';
var editSchoolId = 0;
    
function Load()
{
    Bind(0,0,0,"");
    
    //初始化学校性质和所属校区下拉列表
    InitOptList("queryOption","所有");
    InitAreaList("queryArea","所有");
    InitOptList("popSchoolOpt","请选择");
    InitAreaList("popSchoolArea","请选择");     
}

function Bind(queryType,queryValue_Area,queryValue_Option,queryValue_Name)
{
    if (queryType == 0)
    {
        //初始化学校性质和所属校区下拉列表
        document.getElementById('queryOption').value = 0; 
        document.getElementById('queryArea').value = 0;     
        document.getElementById('queryKeyWord').value = "";   
    }
    
    //查询条件
    document.getElementById('HFQueryType').value = queryType;
    document.getElementById('HFQueryValue_Area').value = queryValue_Area;
    document.getElementById('HFQueryValue_Option').value = queryValue_Option;    
    document.getElementById('HFQueryValue_Name').value = queryValue_Name;    
    document.getElementById('btnBind').click();
}

function InitOptList(selOptId,firstText)
{
    var selectOpt = document.getElementById(selOptId);
    
    //清空
    while(selectOpt.options.length)
    {
        selectOpt.options.remove(0);
    }

    var grades=  ['小学', '初中', '高中', '小学、初中', '初中、高中', '小学、初中、高中'];
    var oOption = document.createElement("OPTION");
    oOption.text=firstText;
    oOption.value= 0;
    selectOpt.add(oOption);
    
    for(var i = 0; i < grades.length;i++)
    {
        var oOption = document.createElement("OPTION");
        oOption.text=grades[i];
        oOption.value=i+1;
        selectOpt.add(oOption);
    } 
    
    selectOpt.value = 0;
}

function  InitAreaList(selAreaId,firstText)
{
    var selectArea = document.getElementById(selAreaId);
    
    //清空
    while(selectArea.options.length)
    {
        selectArea.options.remove(0);
    }

    PageMethods.GetSchoolAreaList(function(succ){
        var oOption = document.createElement("OPTION");
        oOption.text=firstText;
        oOption.value= 0;
        selectArea.add(oOption);
        for(var i = 0; i < succ.length;i++)
        {
            var oOption = document.createElement("OPTION");
            oOption.text=succ[i].SchoolAreaName;
            oOption.value=succ[i].RID;
            selectArea.add(oOption);
        }
        
        selectArea.value = 0;
    },function(err){//异步失败

    })
}

function ShowAddWindow()
{
    optionType = 'add';

    ShowPopForm("popTable", "formTitle", "新增", "");
    showMask();
    
    //初始化学校性质和所属校区下拉列表    document.getElementById('popSchoolOpt').value = 0; 
    document.getElementById('popSchoolArea').value = 0;  
}

function ShowUpdateWindow(obj)
{
    optionType = 'update';

    ShowPopForm("popTable", "formTitle", "修改", "");
    showMask();

    editSchoolId = obj.id;

    PageMethods.SelectSohoolInfo(editSchoolId,function(succ){
        if(succ != null)
        {
            document.getElementById('schoolcode').value = succ.SchoolCode;
            document.getElementById('schoolcode').setAttribute("oldCode",succ.SchoolCode);
            document.getElementById('schoolname').value = succ.SchoolName;
            document.getElementById('schoolname').setAttribute("oldName",succ.SchoolName);
        
            document.getElementById('popSchoolOpt').value = succ.SchoolOpt; 
            document.getElementById('popSchoolArea').value = succ.AreaID;           
        }

    },function(err){//异步失败
        showMassgeBox("提示","获取学校信息失败，请刷新重试",1,null);
    });
}

//删除学校信息
function DelSchoolInfo(obj)
{
    var schoolId = obj.id;

    showConfirmBox("删除", "此操作会删除学校信息，确定删除?", delSchoolInfo, schoolId);
}

function delSchoolInfo(obj)
{
    PageMethods.DeleteSchoolInfo(obj,function(succ){
        if (succ > 0)
        {
            Bind(0,0,0,"");
            showMassgeBox("提示","学校删除成功",1,null);
        }
        else
        {
            showMassgeBox("提示","学校删除失败",1,null);
        }
    },function(err){
        showMassgeBox("提示","学校删除失败，您可以刷新后重试",1,null);
    });
}

//重置学校管理员密码
function ResetAdminPsd(obj)
{
    var schoolId = obj.id;
    
    showConfirmBox("重置密码", "此操作会修改学校管理员密码为初始密码，确定重置密码?", resetAdminPsd, schoolId);
}

function resetAdminPsd(obj)
{
    PageMethods.ResetAdminPsd(obj,function(succ){
        showMassgeBox("提示","重置管理员密码成功",1,null);
    },function(err){
        showMassgeBox("提示","重置管理员密码失败",1,null);
    });
}

function CloseMask(formId)
{
    ReleasePopTable();
    ClosePopForm(formId,true);
    hideMask();
}

//添加或更新操作成功后清空内容
function ReleasePopTable()
{
    document.getElementById('schoolcode').value = "";
    document.getElementById('schoolname').value = ""
    
    document.getElementById('popSchoolOpt').value = 0; 
    document.getElementById('popSchoolArea').value = 0;  
}


function Add(schoolCode,schoolName,schoolOpt,schoolAreaId)
{   
    var schoolInfo = {RID:0,SchoolCode:schoolCode,AreaID:schoolAreaId,SchoolName:schoolName,SchoolOpt:schoolOpt};

    PageMethods.ValidateSchoolInfo(schoolInfo,function(succ){ 
       if(succ > 0)
       {
                if (succ == 1)
                {
                    showNoticMsg('schoolcode','此学校编号已存在，请重新输入');
                    document.getElementById('schoolcode').select();  
                    return;
                }
                
                if (succ == 2)
                {
                    showNoticMsg('schoolcode','此学校编号已作废，请重新输入');
                    document.getElementById('schoolcode').select(); 
                    return; 
                } 
                              
                if (succ == 3)
                {
                    showNoticMsg('schoolname','此学校名称已存在，请重新输入');
                    document.getElementById('schoolname').select();  
                    return;            
                }
                
                if (succ == 4)
                {
                    showNoticMsg('schoolname','此学校名称已作废，请重新输入');
                    document.getElementById('schoolname').select();    
                    return;          
                }
       }
       else
       {  
            PageMethods.InsertSchoolInfo(schoolInfo,function(succ){
                if(succ > 0)
                {
                    Bind(0,0,0,"");
                    CloseMask('popTable');
                }
                else
                {
                    if (succ == -1)
                    {
                        showMassgeBox("提示","学校添加失败，请先设置学期信息",1,null);
                    }
                    if (succ == -2)
                    {
                        showMassgeBox("提示","创建学校数据用户失败，您可以刷新后重试",1,null);
                    }
                }
            },function(err){
                showMassgeBox("提示","学校添加失败，您可以刷新后重试",1,null);
            }); 
       }      
    },function(err){
            showMassgeBox("提示","学校验证过程失败，您可以刷新后重试",1,null);
    });
}

function Update(editSchoolId,schoolCode,schoolName,schoolOpt,schoolAreaId)
{ 
    var schoolInfo = {RID:editSchoolId,SchoolCode:schoolCode,AreaID:schoolAreaId,SchoolName:schoolName,SchoolOpt:schoolOpt};
    
    var oldCode = document.getElementById('schoolcode').getAttribute("oldCode");
    var oldName = document.getElementById('schoolname').getAttribute("oldName");
    //判断学校编号或学校名称是否修改，先验证后更新
    if ( (oldCode != schoolCode) || (oldName != schoolName) )
    {
        PageMethods.ValidateSchoolInfo(schoolInfo,function(succ){ 
           if(succ > 0)
           {
                if (succ == 1)
                {
                    showNoticMsg('schoolcode','此学校编号已存在，请重新输入');
                    document.getElementById('schoolcode').select();  
                    return;
                }
                
                if (succ == 2)
                {
                    showNoticMsg('schoolcode','此学校编号已作废，请重新输入');
                    document.getElementById('schoolcode').select(); 
                    return; 
                } 
                              
                if (succ == 3)
                {
                    showNoticMsg('schoolname','此学校名称已存在，请重新输入');
                    document.getElementById('schoolname').select();  
                    return;            
                }
                
                if (succ == 4)
                {
                    showNoticMsg('schoolname','此学校名称已作废，请重新输入');
                    document.getElementById('schoolname').select();    
                    return;          
                }
                
                if (succ == 5)
                {
                    showNoticMsg('schoolcode','此学校编号正在使用中，不能进行修改操作');
                    document.getElementById('schoolcode').select();              
                }                
           }
           else
           {  
                PageMethods.UpdateSchoolInfo(schoolInfo,function(succ){
                    if(succ > 0)
                    {
                        Bind(0,0,0,"");
                        CloseMask('popTable');
                    }
                    else
                    {
                        showMassgeBox("提示","学校添加失败",1,null);
                    }
                },function(err){
                    showMassgeBox("提示","学校添加失败",1,null);
                }); 
           }
        },function(err){
             showMassgeBox("提示","学校验证过程失败，您可以刷新后重试",1,null);
        });
    }
    //没有修改的情况直接更新
    else
    {
        PageMethods.UpdateSchoolInfo(schoolInfo,function(succ){
            if(succ > 0)
            {
                Bind(0,0,0,"");
                CloseMask('popTable');
            }
            else
            {
                showMassgeBox("提示","学校添加失败",1,null);
            }
        },function(err){
            showMassgeBox("提示","学校添加失败",1,null);
        }); 
    }
}

function Save()
{ 
    var schoolCode = document.getElementById('schoolcode').value.Trim();
    var schoolName = document.getElementById('schoolname').value.Trim();
    var schoolOpt = document.getElementById('popSchoolOpt').value;
    var schoolAreaId = document.getElementById('popSchoolArea').value;    
    
    //判断学校编号是否为空
    if (schoolCode == "")
    {
        showNoticMsg('schoolcode','学校编号不能为空，请重新输入');
        document.getElementById('schoolcode').select();  
        return;  
    }
    if (containChinese(schoolCode))
    {
        showNoticMsg('schoolcode','学校编号只能由英文字母和数字组成');
        document.getElementById('schoolcode').select();  
        return; 
    } 
    if (getStringCount(schoolCode) != 6)
    {
        showNoticMsg('schoolcode','学校编号应输入六个字符，请重新输入');
        document.getElementById('schoolcode').select();  
        return; 
    } 
    if (containSpecialChar(schoolCode) == true)
    {
        showNoticMsg('schoolcode','学校编号不能包含特殊字符，请重新输入');
        document.getElementById('schoolcode').select();  
        return;
    } 
    if (isValidSchoolCode(schoolCode) == false)
    {
        showNoticMsg('schoolcode','学校编号必须以字母开头，由字母和数字组成，请重新输入');
        document.getElementById('schoolcode').select();  
        return;  
    }   
    //判断学校名称是否为空
    if (schoolName == "")
    {
        showNoticMsg('schoolname','学校名称不能为空，请重新输入');
        document.getElementById('schoolname').select();  
        return;  
    }
    if (containSpecialChar(schoolName) == true)
    {
        showNoticMsg('schoolname','学校名称不能包含特殊字符，请重新输入');
        document.getElementById('schoolname').select();  
        return;
    }
    
    //判断是否选择了学校性质
    if (schoolOpt == "0")
    {
        showNoticMsg('popSchoolOpt','请选择学校性质');
        //document.getElementById('popSchoolOpt').select();  
        return;  
    }
    
    //判断是否选择了所属学区
    if (schoolAreaId == "0")
    {
        showNoticMsg('popSchoolArea','请选择所属学区');
        //document.getElementById('popSchoolArea').select();  
        return;  
    }

    
        
    if(optionType == "add")
    {
       Add(schoolCode,schoolName,schoolOpt,schoolAreaId);
    }
    if(optionType == "update")
    {
       Update(editSchoolId,schoolCode,schoolName,schoolOpt,schoolAreaId);
    }
}

function QuerySchool()
{
    var qArea = document.getElementById('queryArea').value;
    var qOption = document.getElementById('queryOption').value;
    var qKeyWord = document.getElementById('queryKeyWord').value.Trim();
    
    if (containSpecialChar(qKeyWord))
    {
        showNoticMsg('queryKeyWord',"查询内容不能包含特殊字符", -150);
        return;
    }
    
    Bind(1,qArea,qOption,qKeyWord);
}

function submitQuery()
{
    if(event.keyCode==13)     
    {     
        event.keyCode=9;   
        event.returnValue = false;
        document.getElementById('btnquery').click();     
    }   
}

function SetHightLight(obj)
{
     obj.style.backgroundColor = "#fffccf";
     var table = document.getElementById('schoolTable');
     for(var i =0 ; i < table.rows.length;i++)
     {
        var row = table.rows[i];
        if(row != null && row.id != obj.id)
            row.style.backgroundColor = "#ffffff";
        
     }
}

function isValidSchoolCode(s)   
{   
    var patrn=/^[a-zA-Z]{1}[a-zA-Z0-9]{5}$/;
    if (!patrn.exec(s)) return false  
    return true  
}  

    </script>
    
</head>
<body onload="Load();">
    <div class="mainWarp">
        <div class="pageBody">
            <form id="form1" runat="server">
                <div>
                    <ul class="butList">
                        <li>
                             <table width="100%" border="0">
                                <tr>
                                    <td  width= "20%" style ="text-align: left;">
                                        <input type="button" id="button3" value="新增" onclick="ShowAddWindow();" class="commonBut" />
                                    </td>
                                    <td width= "80%" style ="text-align: right;">
                                        所属学区:<select id="queryArea" style="width: 120px"></select>&nbsp; 
                                        学校性质:<select id="queryOption" style="width: 120px"></select>&nbsp;
                                        学校名称:<input type="text" name="textfield" id="queryKeyWord" onkeydown="submitQuery();" class="txtStyle" style="width: 120px"/>
                                        &nbsp;<input id="btnquery" class="commonBut" onclick="QuerySchool();" type="button" value="查询" />    
                                    </td>
                                </tr>
                            </table>
                            </li>
                    </ul>
                </div>                
                <div>
                    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
                    </asp:ScriptManager>
                </div>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Repeater ID="Repeater1" runat="server">
                            <HeaderTemplate>
                                <table id="schoolTable" border="0" cellpadding="0" cellspacing="1" class="useList">
                                    <tr class="tableTitle">
                                        <th width="10%" style="text-align: left; padding-left: 5px;">
                                            学校编号</th>
                                        <th width="20%" style="text-align: left; padding-left: 5px;">
                                            学校名称</th>
                                        <th width="20%" style="text-align: left; padding-left: 5px;">
                                            学校性质</th>
                                        <th width="20%" style="text-align: left; padding-left: 5px;">
                                            学区</th>
                                        <th width="20%" style="text-align: left; padding-left: 5px;">
                                            管理员</th>
                                        <th width="10%">
                                        </th>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr id="<%# DataBinder.Eval(Container.DataItem, "RID") %>" 
                                    style="background-color: White;"  onclick="SetHightLight(this);">
                                    <td style="text-align: left; font-weight: normal;padding-left:5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "SchoolCode")%>
                                    </td>
                                    <td style="text-align: left; font-weight: normal;padding-left:5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "SchoolName")%>
                                    </td>
                                    <td style="text-align: left; font-weight: normal; padding-left: 5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "SchoolOptName")%>
                                    </td>
                                    <td style="text-align: left; font-weight: normal; padding-left: 5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "SchoolAreaName")%>
                                    </td>
                                    <td style="text-align: left; font-weight: normal; padding-left: 5px;">
                                        <%# DataBinder.Eval(Container.DataItem, "AdminName")%>
                                    </td>
                                    <td>
                                        <img src="../common/images/resetPwd.gif" alt="重置管理员密码" class="imageBtnStyle" onclick="ResetAdminPsd(this.parentElement.parentElement);" />
                                        <img src="../common/images/hadle_icon.gif" alt="修改学校信息" class="imageBtnStyle" onclick="ShowUpdateWindow(this.parentElement.parentElement);" />
                                        <img src="../common/images/delete_icon.gif" alt="删除学校信息" class="imageBtnStyle" onclick="DelSchoolInfo(this.parentElement.parentElement);" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                         <webdiyer:aspnetpager id="AspNetPager1" runat="server" horizontalalign="Right"  PageSize="15" 
                            width="100%"   PagingButtonSpacing="10px" ButtonImageAlign="left" 
                            OnPageChanged="AspNetPager1_PageChanged"> </webdiyer:aspnetpager> 
                        </td> </tr> </table>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnBind" EventName="Click" />
                    </Triggers>
                </asp:UpdatePanel>
                <div style="display: none;">
                    <asp:Button ID="btnBind" runat="server" OnClick="btnBind_Click" Text="btnBind" />
                    <asp:HiddenField ID="HFQueryType" runat="server" Value="" />
                    <asp:HiddenField ID="HFQueryValue_Area" runat="server" Value="" />
                    <asp:HiddenField ID="HFQueryValue_Option" runat="server" Value="" />
                    <asp:HiddenField ID="HFQueryValue_Name" runat="server" Value="" />                    
                </div>
            </form>
            <!-- 新增/修改学校弹出界面 开始-->
            <div id="popTable" class="layoutWarp" style="display: none; width: 400px; overflow: inherit;">
                <div class="title">
                    <span id="formTitle">新增</span> <a title="关闭" onclick="return CloseMask('popTable')">
                        关闭</a>
                </div>
                <table style="width: 380px; margin-top: 12px;" border="0" class="layoutTable">
                    <tr>
                        <td width="35%" valign="top" style="text-align: right">
                             学校编号:</td>
                        <td style="text-align: left; width: 243px; color:Red">
                            <input type="text" name="textfield" id="schoolcode" style="width: 120px" maxlength = "6" class="txtStyle" /> *</td>
                    </tr>
                    <tr>
                        <td valign="top" style="text-align: right">
                            学校名称:</td>
                        <td style="text-align: left; width: 243px; color:Red">
                            <input id="schoolname" name="textfield2" style="width: 120px" type="text" maxlength = "25" class="txtStyle" /> *</td>
                    </tr>
                    <tr>
                        <td style="text-align: right" valign="top" width="35%">
                            学校性质:</td>
                        <td style="text-align: left; width: 243px; color:Red">
                            <select id="popSchoolOpt" style="width: 123px">                        
                            </select> *</td>
                    </tr>
                    <tr>
                        <td style="text-align: right" valign="top">
                            所属学区:</td>
                        <td style="text-align: left; width: 243px; color:Red">
                            <select id="popSchoolArea" style="width: 123px">                        
                            </select> *</td>
                    </tr>                    
                    <tr>
                        <td colspan="3" align="center">
                            <ul class="butList">
                                <input type="button" id="btnsave" value="保存" onclick="Save()" class="commonBut" />
                                <input type="button" value="取消" onclick=" CloseMask('popTable');" class="commonBut" />
                            </ul>
                        </td>
                    </tr>
                </table>
            </div>
            <!-- 新增/修改学校弹出界面 结束 -->
        </div>
    </div>
</body>
</html>
